<?php
/**
 * Define constants for paths and other global metadata.
 * 
 * @package Mkk
 * @subpackage Mkk_Application
 * @category Application
 * @author phucnh
 * @since $Id: path_config.php 2158 2010-11-01 09:59:18Z CYBERSOFT\phucnh $
 */

// Report all errors except E_NOTICE.
error_reporting(E_ALL ^ E_NOTICE);

@mb_http_output( "UTF-8" );
@mb_detect_order( "UTF-8,UTF-7,ISO-2022-JP,JIS,SJIS-win,SJIS,eucJP-win,EUC-JP,ASCII" );
@header( "Content-type: text/html; charset=UTF-8" );

// Set the zlib config values if the extension has been loaded.
// Zip level 1: lowest, 9: highest
if (extension_loaded('zlib')) {
    ini_set('zlib.output_compression', true);
    ini_set('zlib.output_compression_level', '9');    
}

/**
 * Defines the directory separator for windows or unix env
 */
define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);

// Document Root without / at the end
defined('APPLICATION_ROOT') || define('APPLICATION_ROOT', realpath(APPLICATION_PATH . '/../'));
// Module Path
defined('MODULE_PATH') || define('MODULE_PATH', APPLICATION_PATH . DS .'modules');

// Language Files Path
defined('LANG_PATH') || define('LANG_PATH', APPLICATION_PATH . DS .'langs');

// Library Path
defined('LIBRARY_PATH') || define('LIBRARY_PATH', APPLICATION_ROOT . DS .'library');

// Data Path
defined('DATA_PATH') || define('DATA_PATH', APPLICATION_ROOT . DS .'data');

// Lock file Path - set full right access
defined('LOCK_PATH') || define('LOCK_PATH', DATA_PATH . DS .'lock');

// Log file Path - set full right access
defined('LOG_PATH') || define('LOG_PATH', DATA_PATH . DS .'logs');

// Cache Path - set full right access
defined('CACHE_PATH') || define('CACHE_PATH', DATA_PATH . DS .'caches');

// Lucence search data file Path - set full right access
defined('INDEX_PATH') || define('INDEX_PATH', DATA_PATH . DS .'indexes');

// Session Path - set full right access
defined('SESSION_PATH') || define('SESSION_PATH', DATA_PATH . DS .'sessions');

// Web Path - location where the web is published
defined('WEB_PATH') || define('WEB_PATH', APPLICATION_ROOT . DS. 'public');

// Path contains Site contents, logo images, so on
defined('SITE_DATA_PATH') || define('SITE_DATA_PATH', 
                                     DATA_PATH . DS . 'uploads' . DS. 'site');

// Upload Ad Photo Path - set full right access
defined('UPLOAD_AD_PHOTO_PATH') || define('UPLOAD_AD_PHOTO_PATH',
                                     DATA_PATH . DS .'uploads' . DS . 'ads');

// Upload Ad Photo Path - set full right access
defined('AD_PHOTO_PATH') || define('AD_PHOTO_PATH',
                                   WEB_PATH . DS .'img' . DS . 'items');

// application.ini cache file
defined('INI_FILE')
    || define('INI_FILE',  APPLICATION_PATH . DS .'configs/application.ini');
    
// application.ini.php cache file
defined('INI_CACHED_FILE')
    || define('INI_CACHED_FILE',  CACHE_PATH . DS .'application.ini.php');
    
// Define the web address constants:
defined('WEB_ROOT') || _define_web_root(); 
defined('WEB_SKIN') || define('WEB_SKIN', WEB_DIR . '/skins');
if (!isCliMode()) {
  defined('WEB_HOST') || define('WEB_HOST', parse_url(WEB_ROOT, PHP_URL_HOST));
}

defined('CACHE_CONFIG') || define('CACHE_CONFIG', FALSE);

/**
 * overwrite autoload function
 *
 * @access private
 * @param string $className class name
 * @return void
 */
function __autoload($className) 
{
    include_once str_replace('_', '/', $className) . '.php';
    return $className;
}

/**
 * Check if php is running at the command line
 * 
 * @return boolean TRUE if php is running in CLI mode
 * @see http://www.codediesel.com/php/quick-way-to-determine-if-php-is-running-at-the-command-line/
 */
function isCliMode() 
{
     if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
          return true;
     } else {
          return false;
     }
}

/**
 * Most of this has been borrowed directly from Drupal 6.1's 
 * bootstrap code (bootstrap.inc, conf_init())
 *
 * @access private
 * @todo rename the WEB_ROOT, WEB_DIR constants and add a new one for the APPLICATION_ROOT
 * @return void
 */
function _define_web_root()
{
    // Create base URL
    $baseRoot = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
    
    // As $_SERVER['HTTP_HOST'] is user input, ensure it only contains 
    // characters allowed in hostnames.
    $baseUrl = $baseRoot .= '://' . preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']);
    
    // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not
    // be modified by a visitor.
    $dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/');
    if ($dir) {
        $appRoot  = "/" . $dir;
        $baseUrl .= $appRoot;
        $appRoot .= '/';
    } else {
        $appRoot = '/';
    }
        
    // WEB_ROOT is always the root of the site, whereas WEB_DIR depends on the 
    // bootstrap used (public/admin)
    
    // @hack Remove the '/admin' part of the URL by regex (only if necessary)
    if (defined('ADMIN')) {  
        $dir = preg_replace('/(.*)admin$/', '$1', $dir, 1);
        $dir = rtrim($dir, '/');
    }
   
   define('WEB_ROOT', $baseRoot . (!empty($dir) ? '/' . $dir : '') );
   define('WEB_DIR',  $baseUrl);  
}

// Get the directory that the bootstrap sits in.
$dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/');

// current path should be empty and not a '/' if there is no directory path.
$currentPath = !empty($dir) ? "/" . $dir : "";
$adminDir = $site['admin'];

// This is how we determine whether or not we are in the admin bootstrap.
if (defined('ADMIN')) {
    $adminPath = $currentPath;
    // Strip off the admin directory to get the public dir.
    // @hack Remove the '/admin' part of the URL by regex (only if necessary).
    $publicPath = rtrim(preg_replace("/(.*)" . $adminDir . "$/", '$1', $currentPath, 1), '/');
} else {
    $adminPath = $currentPath . "/" . $adminDir;
    $publicPath = $currentPath;
}

// reset include paths
$paths = array();
$paths[] = LIBRARY_PATH;
// @todo check if nessessary to improve performance
$paths[] = get_include_path();
$includePath = implode($paths, PS);
set_include_path($includePath);